package receive

import (
	"WeChatMGRA/modul"
	"encoding/json"
	"fmt"
	"gitee.com/yutiandou/wdtools/wdutil"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
)
var stat bool = true
var Wid string = ""
var msgchan = make(chan *modul.VoiceDown,1)

func ReceiveGroup(){
	go func() {
		monserver := http.NewServeMux()
		monserver.HandleFunc("/test", testhandler)
		monserver.HandleFunc("/hello",func(w http.ResponseWriter,r * http.Request){
			w.Write([]byte("hello world"))
		})
		http.ListenAndServe("127.0.0.1:80", monserver)
	}()
	SetServerHandler()
}
func testhandler(w http.ResponseWriter,r * http.Request){
	data,err:=ioutil.ReadAll(r.Body)
	if err!=nil{
		fmt.Println(err)
		return
	}
	//fmt.Println(string(data))
	msg := new(modul.ReceiveInfo)
	err=json.Unmarshal(data,msg)
	if err!=nil{
		fmt.Println(err)
		return
	}
	fmt.Println("发送者：",msg.Message.Data.FromUser,"消息源：",msg.Message.Data.MsgSource)
	if msg.Message.MessageType == 2 && msg.Message.Data.Type == 3 && msg.Message.Data.FromUser=="wxid_59pqhmm1h28821"{
	 	fmt.Println("接到一条认定消息")
		msgdown := new(modul.VoiceDown)
		msgdown.WID = Wid
		msgdown.MsgId = msg.Message.Data.MsgId
		leng := wdutil.RegexpFromStringS(msg.Message.Data.Content,` length="(?s:(.*?))" `,1)
		msglen,_:= strconv.ParseFloat(leng[0][1],32)
		//fmt.Println(msg.Message.Data.Content,"长度---》",leng[0][0],"-长度-",msglen)
		msgdown.Length = int(msglen)
		msgchan<-msgdown
		//stat = false
	}else{
		fmt.Println("接到一条无名消息")
	}
}

func SetServerHandler() {
	req, err := http.NewRequest("POST", "http://134.175.73.113:8080/setHttpCallbackUrl", strings.NewReader("{\"httpUrl\":\"http://220c4c80.nat123.cc/test\"}"))
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6ImUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNlRWJLODcxRFZDTGhJbCh6TyIsImlzcyI6InhpbmdzaGVuZyIsImFjY291bnQiOiIxODcyMjYyNjExMCJ9._GDQ5kANgtC8Zon0_3BAp-Jz0k21H58Jq8e_CU4fjjc")
	if err != nil {
		fmt.Println("退出数据错误")
		return
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("设置url失败")
	}
	data,err:=ioutil.ReadAll(res.Body)
	if err!=nil{
		fmt.Println("设置url失败")
		return
	}
	basemsg := new(modul.BaseMsg)
	err =json.Unmarshal(data,basemsg)
	if err!=nil{
		fmt.Println("设置url失败")
		return
	}
	if basemsg.Code == "1000"{
		fmt.Println("设置url成功")
	}else{
		fmt.Println("设置url失败")
	}
}

func Downmsg()(string,int){
	ctx := <- msgchan
	ctxstr,_ := json.Marshal(ctx)
	req, err := http.NewRequest("POST", "http://134.175.73.113:8080/getMsgVoice", strings.NewReader(string(ctxstr)))
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6ImUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNlRWJLODcxRFZDTGhJbCh6TyIsImlzcyI6InhpbmdzaGVuZyIsImFjY291bnQiOiIxODcyMjYyNjExMCJ9._GDQ5kANgtC8Zon0_3BAp-Jz0k21H58Jq8e_CU4fjjc")
	if err != nil {
		fmt.Println("获取数据错误")
		return "",0
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("获取请求错误")
	}
	data,err:=ioutil.ReadAll(res.Body)
	if err!=nil{
		fmt.Println("获取错误")
		return "",0
	}
	basemsg := new(modul.BaseMsg)
	err =json.Unmarshal(data,basemsg)
	if err!=nil{
		fmt.Println("获取数据解析错误")
		return "",0
	}
		fmt.Println(basemsg)

	return basemsg.Data.(string),ctx.Length
}